<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :page="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      v-model="form"
      ref="crud"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @row-del="rowDel"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template slot="menuLeft">
        <el-button
          type="danger"
          size="small"
          icon="el-icon-delete"
          plain
          v-if="permission.customer_delete"
          @click="handleDelete"
        >删 除</el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import {
  getList,
  getDetail,
  add,
  update,
  remove,
  getTreeList,
} from "@/api/meter/customer";
import { mapGetters } from "vuex";

export default {
  data() {
    return {
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      selectionList: [],
      option: {
        height: "auto",
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        index: true,
        viewBtn: true,
        selection: true,
        dialogClickModal: false,
        labelWidth: 150,
        column: [
          {
            label: "父级编号",
            prop: "parentId",
            rules: [
              {
                required: true,
                message: "请输入父级编号",
                trigger: "blur",
              },
            ],
          },
          {
            label: "客户编号",
            prop: "customerNo",
            rules: [
              {
                required: true,
                message: "请输入客户编号",
                trigger: "blur",
              },
            ],
          },
          {
            label: "客户名称",
            prop: "customerName",
            rules: [
              {
                required: true,
                message: "请输入客户名称",
                trigger: "blur",
              },
            ],
          },
          {
            label: "客户类型",
            prop: "customerType",
            type: "select",
            dicUrl: "/api/blade-system/dict-biz/dictionary?code=customer_type",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            rules: [
              {
                required: true,
                message: "请输入客户类型",
                trigger: "blur",
              },
            ],
          },
          {
            label: "客户类别",
            prop: "customerClass",
            type: "select",
            dicUrl: "/api/blade-system/dict-biz/dictionary?code=customer_class",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            rules: [
              {
                required: true,
                message: "请输入客户类别",
                trigger: "blur",
              },
            ],
          },
          {
            label: "注册资金",
            prop: "registeredCapital",
            rules: [
              {
                required: false,
                message: "请输入注册资金",
                trigger: "blur",
              },
            ],
          },
          {
            label: "经营范围",
            prop: "businessScope",
            rules: [
              {
                required: false,
                message: "请输入经营范围",
                trigger: "blur",
              },
            ],
          },
          {
            label: "主要产品",
            prop: "products",
            rules: [
              {
                required: false,
                message: "请输入主要产品",
                trigger: "blur",
              },
            ],
          },
          {
            label: "企业简介",
            prop: "companyInfo",
            rules: [
              {
                required: false,
                message: "请输入企业简介",
                trigger: "blur",
              },
            ],
          },
          {
            label: "销售额",
            prop: "sales",
            rules: [
              {
                required: false,
                message: "请输入销售额",
                trigger: "blur",
              },
            ],
          },
          {
            label: "运行容量",
            prop: "runCapacity",
            rules: [
              {
                required: false,
                message: "请输入运行容量",
                trigger: "blur",
              },
            ],
          },
          {
            label: "风险等级",
            prop: "dangerLevel",
            type: "select",
            dicUrl: "/api/blade-system/dict-biz/dictionary?code=danger_level",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            rules: [
              {
                required: false,
                message: "请输入风险等级",
                trigger: "blur",
              },
            ],
          },
          {
            label: "行业分类",
            prop: "industryClass",
            type: "select",
            dicUrl: "/api/blade-system/dict-biz/dictionary?code=industry_class",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            rules: [
              {
                required: false,
                message: "请输入行业分类",
                trigger: "blur",
              },
            ],
          },
          {
            label: "高可靠性标志",
            prop: "reliabilityFlag",
            rules: [
              {
                required: false,
                message: "请输入高可靠性标志",
                trigger: "blur",
              },
            ],
          },
          {
            label: "负荷性质",
            prop: "loadNature",
            rules: [
              {
                required: false,
                message: "请输入负荷性质",
                trigger: "blur",
              },
            ],
          },
          {
            label: "信用分值",
            prop: "credit",
            rules: [
              {
                required: false,
                message: "请输入信用分值",
                trigger: "blur",
              },
            ],
          },
          {
            label: "信用等级",
            prop: "creditLevel",
            type: "select",
            dicUrl: "/api/blade-system/dict-biz/dictionary?code=credit_level",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            rules: [
              {
                required: false,
                message: "请输入信用等级",
                trigger: "blur",
              },
            ],
          },
          {
            label: "用电类型",
            prop: "powerType",
            type: "select",
            dicUrl: "/api/blade-system/dict-biz/dictionary?code=power_type",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            rules: [
              {
                required: false,
                message: "请输入用电类型",
                trigger: "blur",
              },
            ],
          },
          {
            label: "供电单位",
            prop: "supplyCompany",
            rules: [
              {
                required: false,
                message: "请输入供电单位",
                trigger: "blur",
              },
            ],
          },
          {
            label: "转供电类型",
            prop: "transferSupplyType",
            type: "select",
            dicUrl:
              "/api/blade-system/dict-biz/dictionary?code=transfer_supply_type",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            rules: [
              {
                required: false,
                message: "请输入转供电类型",
                trigger: "blur",
              },
            ],
          },
          {
            label: "所属区域",
            prop: "regionId",
            // lazy: true,
            type: "tree",
            dicUrl: "/api/blade-system/region/tree",
            props: {
              label: "title",
              value: "id",
              leaf: "leaf",
              disabled: "disabled",
            },
            // 地区懒加载
            // treeLoad: function(node, resolve) {
            //   if (node.level === 0) {
            //     getTreeList().then(res => {
            //       const data = res.data.data;
            //       data.forEach(item => {
            //         if (!item.hasChildren) {
            //           item.leaf = true;
            //         } else {
            //           item.disabled = true;
            //         }
            //       });
            //       return resolve(res.data.data);
            //     });
            //   }
            //   if (node.data.hasChildren) {
            //     getTreeList(node.data.id).then(res => {
            //       const data = res.data.data;
            //       data.forEach(item => {
            //         if (!item.hasChildren) {
            //           item.leaf = true;
            //         } else {
            //           item.disabled = true;
            //         }
            //       });
            //       return resolve(res.data.data);
            //     });
            //   }
            // },
            rules: [
              {
                required: true,
                message: "请输入所属区域",
                trigger: "blur",
              },
            ],
          },
          {
            label: "客户经理",
            prop: "managerId",
            rules: [
              {
                required: false,
                message: "请输入客户经理",
                trigger: "blur",
              },
            ],
          },
          {
            label: "联系人姓名",
            prop: "contactName",
            rules: [
              {
                required: true,
                message: "请输入联系人姓名",
                trigger: "blur",
              },
            ],
          },
          {
            label: "联系人分类",
            prop: "contactClass",
            type: "select",
            dicUrl: "/api/blade-system/dict-biz/dictionary?code=contact_class",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            rules: [
              {
                required: true,
                message: "请输入联系人分类",
                trigger: "blur",
              },
            ],
          },
          {
            label: "性别",
            prop: "gender",
            type: "select",
            dicUrl: "/api/blade-system/dict/dictionary?code=sex",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            rules: [
              {
                required: false,
                message: "请输入性别",
                trigger: "blur",
              },
            ],
          },
          {
            label: "联系电话",
            prop: "contactTel",
            rules: [
              {
                required: true,
                message: "请输入联系电话",
                trigger: "blur",
              },
            ],
          },
          {
            label: "联系地址",
            prop: "address",
            rules: [
              {
                required: false,
                message: "请输入联系地址",
                trigger: "blur",
              },
            ],
          },
          {
            label: "证件类型",
            prop: "identifyType",
            type: "select",
            dicUrl: "/api/blade-system/dict-biz/dictionary?code=identify_type",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            rules: [
              {
                required: false,
                message: "请输入证件类型",
                trigger: "blur",
              },
            ],
          },
          {
            label: "证件号码",
            prop: "identifyNo",
            rules: [
              {
                required: false,
                message: "请输入证件号码",
                trigger: "blur",
              },
            ],
          },
          {
            label: "备注信息",
            prop: "remarks",
            rules: [
              {
                required: false,
                message: "请输入备注信息",
                trigger: "blur",
              },
            ],
          },
        ],
      },
      data: [],
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.customer_add, false),
        viewBtn: this.vaildData(this.permission.customer_view, false),
        delBtn: this.vaildData(this.permission.customer_delete, false),
        editBtn: this.vaildData(this.permission.customer_edit, false),
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(",");
    },
  },
  methods: {
    rowSave(row, done, loading) {
      add(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          loading();
          window.console.log(error);
        }
      );
    },
    rowUpdate(row, index, done, loading) {
      update(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          loading();
          console.log(error);
        }
      );
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        getDetail(this.form.id).then((res) => {
          this.form = {
            ...res.data.data,
            regionId: res.data.data.regionId.toString(),
            customerType: res.data.data.customerType.toString(),
            customerClass: res.data.data.customerClass.toString(),
            dangerLevel: res.data.data.dangerLevel.toString(),
            industryClass: res.data.data.industryClass.toString(),
            creditLevel: res.data.data.creditLevel.toString(),
            powerType: res.data.data.powerType.toString(),
            transferSupplyType: res.data.data.transferSupplyType.toString(),
            contactClass: res.data.data.contactClass.toString(),
            gender: res.data.data.gender.toString(),
            identifyType: res.data.data.identifyType.toString(),
          };
        });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query)
      ).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records.map((item) => {
          return {
            ...item,
            regionId: item.regionId.toString(),
            customerType: item.customerType.toString(),
            customerClass: item.customerClass.toString(),
            dangerLevel: item.dangerLevel.toString(),
            industryClass: item.industryClass.toString(),
            creditLevel: item.creditLevel.toString(),
            powerType: item.powerType.toString(),
            transferSupplyType: item.transferSupplyType.toString(),
            contactClass: item.contactClass.toString(),
            gender: item.gender.toString(),
            identifyType: item.identifyType.toString(),
          };
        });
        this.loading = false;
        this.selectionClear();
      });
    },
  },
};
</script>

<style>
</style>
